import { clear_gun, get_gun } from 'base/api';
import { info_data, msn_info, info_rm } from 'use/info';
import { s_psn, g_db } from 'db/state';
import { put_log } from 'base/ctl/log';
import { obj_str, uJob, cline } from 'sui';
import { ck_pos } from './code';
import { ck_range } from 'client/away/data/base';

const do_away = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描外圈轴承编码");

    clear_gun();

    s_psn(psn);
    g_db.s_msn("");

    umd.mset({
        "psn": psn,
        "msn": "",
    });

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const [e, pos] = ck_pos(psn);
    if (e) {
        return herr(e);
    }

    const box_sn = g_db.box();

    uJob(null, "away/move",
        {
            line: cline(),
            k: psn,
            pos: pos,
            box: box_sn,
            tech: g_db.tech(),
        },
        r => {
            if (r === "nodata") {
                return herr("无数据，轴承已经提交到工单 nodata, 请继续扫下一个轴承码");
            } else if (r === "noall") {
                return herr("数据不全，轴承没有进入后制成库, 请继续扫下一个轴承码");
            } else if (r === "notech") {
                return herr("型号不匹配, 请继续扫下一个轴承码");
            }

            const tech = obj_str(r, "tech");

            g_db.s_tech(tech);
            umd.set("tech", tech);

            const is_new = msn_info(psn, pos);

            if (is_new) {
                let num = g_db.num() + 1;
                let sum = g_db.sum() + 1;

                g_db.s_num(num);
                g_db.s_sum(sum);

                umd.mset({
                    sum: sum,
                    num: num,
                    err: "",
                });
            }
            watch.startWatch();
        },
        e => herr("产品号绑定箱号出错"));
};

const do_badin = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描外圈轴承编码");

    clear_gun();
    s_psn(psn);

    umd.set("psn", psn);

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const [e, pos, sum] = ck_pos(psn);
    if (e) {
        return herr(e);
    }

    const tech = g_db.tech();
    if (!tech) {
        return herr("型号没有设置");
    }

    uJob(null, "work/nodata",
        {
            line: cline(),
            sn: psn,
            tech: tech,
            box: g_db.box(),
            pos: pos,
        },
        r => {
            msn_info(psn, pos);

            let num = g_db.num() + 1;

            g_db.s_num(num);
            g_db.s_sum(sum);

            umd.mset({
                sum: sum,
                num: num,
                err: "",
            });

            watch.startWatch();
        },
        e => herr("产品号不存在"));
};

const do_pout = (stepnum, step, umd, watch) => {
    const psn = get_gun(step);
    if (!psn) {
        watch.startWatch();
        return;
    }

    put_log("编码", psn, "扫描编码");

    clear_gun();

    s_psn(psn);

    umd.mset({
        "psn": psn,
    });

    const herr = (e) => {
        umd.setErr(e);
        watch.startWatch();
    };

    const box_sn = g_db.box();

    const val = info_data(umd, psn);
    if (!val) {
        return herr("此编码不在箱子当中");
    }

    const opt = {
        dia_min: g_db.dia_min(),
        dia_max: g_db.dia_max(),
        dt_min: g_db.dt_min(),
        dt_max: g_db.dt_max(),
        w_min: g_db.w_min(),
        w_max: g_db.w_max(),
    };

    if (!ck_range(opt, val)) {
        return herr("此编码数据不合格，不应该被挑出");
    }

    uJob(null, "away/outbox",
        {
            line: cline(),
            k: psn,
            tp: g_db.boxtp(),
            box: box_sn,
        },
        r => {
            umd.mset({
                err: "",
            });

            info_rm(umd, psn);

            watch.startWatch();
        },
        e => herr("产品号绑定箱号出错"));
};

export {do_pout, do_badin, do_away};
